package cn.laowen.basic.controller;


import cn.laowen.basic.dto.RegisterDto;
import cn.laowen.basic.exception.BusinessException;
import cn.laowen.basic.service.IVerifyCodeService;
import cn.laowen.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 发送验证码
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 发送图形验证码
     * @param key
     * @return
     */
    @GetMapping("/image/{imageCodeKey}") //所有要获取图片验证码都可以使用
    public AjaxResult imgCode(@PathVariable("imageCodeKey")String key){
        try {
            String base64 = verifyCodeService.getImgCode(key);
            return AjaxResult.me().setResultObj(base64);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     * 发送短信验证码
     * @param registerDto
     * @return
     */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody RegisterDto registerDto){
        try {
            verifyCodeService.smsCode(registerDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
}
